using System;
using System.Collections.Generic;
using System.Text;
using PluginHandler.Database;
using MySql.Data.MySqlClient;
using System.Data;

namespace MySqlPlugin.Database
{
    public class DatabaseStructure : IDatabaseStructure
    {
        private DatabaseHandler _databaseHandler;
        private List<IDatabaseTable> _tables;
        private List<IDatabaseView> _views;

        internal DatabaseStructure(DatabaseHandler databaseHandler)
        {
            _databaseHandler = databaseHandler;

            _tables = null;
            _views = null;
        }

        #region IDatabaseStructure Member

        public List<IDatabaseTable> Tables
        {
            get 
            {
                if (_tables == null)
                {
                    _tables = new List<IDatabaseTable>();

                    MySqlConnection mySqlConnection = (MySqlConnection)_databaseHandler.DatabaseConnection;
                    DataTable dataTable = mySqlConnection.GetSchema(
                        "Tables",
                        new string[] 
                        { 
                            null, 
                            _databaseHandler.ConnectionStringDetails["Database"].ToString(), 
                            null, 
                            "BASE TABLE" 
                        });

                    foreach (DataRow dataRow in dataTable.Rows)
                    {
                        string tableName = dataRow["TABLE_NAME"].ToString();
                        _tables.Add(new DatabaseTable(_databaseHandler, tableName));
                    }
                }

                return _tables;
            }
        }

        public List<IDatabaseView> Views
        {
            get 
            {
                if (_views == null)
                {
                    _views = new List<IDatabaseView>();

                    MySqlConnection mySqlConnetion = (MySqlConnection)_databaseHandler.DatabaseConnection;
                    DataTable dataTable = mySqlConnetion.GetSchema(
                        "Views",
                        new string[] 
                        { 
                            null,
                            _databaseHandler.ConnectionStringDetails["Database"].ToString()
                        });

                    foreach (DataRow dataRow in dataTable.Rows)
                    {
                        string viewName = dataRow["TABLE_NAME"].ToString();

                        _views.Add(new DatabaseView(_databaseHandler, viewName));
                    }
                }

                return _views;
            }
        }

        public bool ViewsSupported
        {
            get 
            {

                return true;
            }
        }

        public bool TablesSupported
        {
            get 
            {
                return true;
            }
        }

        #endregion
    }
}
